import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/user/login.vue'
import home from '@/views/home/home.vue'
import qa from '@/views/qa/qa.vue'
import movie from '@/views/movie/movie.vue'
import user from '@/views/user/user.vue'
import student from '@/views/user/student.vue'
import settings from '@/views/user/settings.vue'
import notfound from '@/views/notfound/notfound.vue'
import edit from '@/views/user/edit.vue'
import myarticle from '@/views/home/myarticle.vue'
import search from '@/views/home/search.vue'
import searchResult from '@/views/home/searchResult.vue'

Vue.use(VueRouter)

const routes = [
  {
    name: 'login',
    path: '/login',
    component: login
    
  }, 
  {
    name: 'settings',
    path: '/settings',
    component: settings
    
  },   {
    name: 'student',
    path: '/student',
    component: student
    
  }, 
  {
    name: 'home',
    path: '/home',
    component: home,
    meta:{showTab:true,isLogin:true}
  },
  {
    name: 'qa',
    path: '/qa',
    component: qa,
    meta:{showTab:true}
  },
  {
    name: 'movie',
    path: '/movie',
    component: movie,
    meta:{showTab:true}
  },
  {
    name: 'user',
    path: '/user',
    component: user,
    meta:{showTab:true}
  },
  {
    name: 'edit',
    path: '/edit',
    component: edit,
  },
   {
    name: 'myarticle',
    path: '/myarticle',
    component: myarticle,
  },
  {
    name: 'search',
    path: '/search',
    component: search,
  },
  ,
  {
    name: 'searchResult',
    path: '/searchResult',
    component: searchResult,
  },
{
  //注册组件，注册才能跳转
  name:'notfound',
  path:'/notfound',
  component: notfound,

},
//首页重定向
{
  path:'/',
  redirect:'/home'
},
{
  path:'/*',
  redirect:'/notfound'
},
]
const router = new VueRouter({
  routes
})
//导入token包
import {getToken} from '@/utils/token.js'
router.beforeEach((to,from,next)=>{
  console.log(getToken);
  if(to.meta.isLogin&&!getToken()){
    next({path:'/login'})
  }else{
    next()
  }
})


export default router
